package com.xs.client.entity;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import io.netty.channel.ChannelPromise;

public class RpcFuture {

	private Lock lock = new ReentrantLock();

	private Condition wait = lock.newCondition();

	private RpcRequest rpcRequest;

	private volatile ChannelPromise promise;

	private volatile RpcResponse rpcResponse;

	public ChannelPromise getPromise() {

		return promise;

	}

	public void setPromise(ChannelPromise promise) {
		this.promise = promise;
	}

	public RpcFuture(RpcRequest rpcRequest, ChannelPromise promise) {
		super();
		this.rpcRequest = rpcRequest;
		this.promise = promise;
	}

	public RpcRequest getRpcRequest() {
		return rpcRequest;
	}

	public void setRpcRequest(RpcRequest rpcRequest) {

		this.rpcRequest = rpcRequest;

	}

	public RpcResponse getRpcResponse() {
		lock.lock();
		try {
			wait.await(3, TimeUnit.MINUTES);
		} catch (InterruptedException e) {
			e.printStackTrace();
		} finally {
			lock.unlock();
		}
		return rpcResponse;

	}

	public void setRpcResponse(RpcResponse rpcResponse) {

		lock.lock();
		try {
			wait.signal();
			this.rpcResponse = rpcResponse;
		} finally {
			lock.unlock();
		}

	}

}
